package com.cskaoyan.service;


import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.util.LocalStorage;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class AdminStorageServletImpl implements AdminStorageService {
    private LocalStorage storage = new LocalStorage();
    @Override
    public MarketStorage store(InputStream inputStream, long size, String contentType, String fileName) {
        String key = generateKey(fileName);
        storage.store(inputStream, size, contentType, key);

        String url = generateUrl(key);
        MarketStorage storageInfo = new MarketStorage();
        storageInfo.setName(fileName);
        storageInfo.setSize((int) size);
        storageInfo.setType(contentType);
        storageInfo.setKey(key);
        storageInfo.setUrl(url);
        this.add(storageInfo);

        return storageInfo;
    }

    @Override
    public int add(MarketStorage storageInfo) {
        Date now = new Date();
        storageInfo.setAddTime(now);
        storageInfo.setUpdateTime(now);
        MarketStorageMapper storageMapper = TransactionUtil.getMapper(MarketStorageMapper.class);
        int insert = storageMapper.insert(storageInfo);
        TransactionUtil.commit();
        return insert;
    }

    private String generateUrl(String key) {

        return "http://localhost:8083/admin/storage/fetch/" + key;
    }

    private String generateKey(String originalFilename) {
        int index = originalFilename.lastIndexOf('.');
        String suffix = originalFilename.substring(index);

        String key = UUID.randomUUID().toString().replaceAll("-","") + suffix;

        return key;
    }

    @Override
    public BaseRespVo list(HttpServletRequest request) {
        String pageStr = request.getParameter("page");
        String oder = request.getParameter("order");
        Integer page = Integer.parseInt(pageStr);
        String limitStr = request.getParameter("limit");
        Integer limit = Integer.parseInt(limitStr);
        String sort = request.getParameter("sort");
        String key = request.getParameter("key");
        String name = request.getParameter("name");


        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper mapper = sqlSession.getMapper(MarketStorageMapper.class);
        PageHelper.startPage(page,limit);
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (key!=null&&!"".equals(key))
            criteria.andKeyEqualTo(key);
        if (name!=null&&!"".equals(name))
            criteria.andNameEqualTo(name);

        List<MarketStorage> marketStorages = mapper.selectByExample(marketStorageExample);
        CommonData data = CommonData.data(marketStorages);

        sqlSession.close();
        return BaseRespVo.ok(data);

    }

    @Override
    public BaseRespVo update(MarketStorage marketStorage) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper mapper = sqlSession.getMapper(MarketStorageMapper.class);
        mapper.updateByPrimaryKey(marketStorage);
        MarketStorage marketStorage1 = mapper.selectByPrimaryKey(marketStorage.getId());

        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok(marketStorage1);
    }

    @Override
    public BaseRespVo delete(MarketStorage marketStorage) {

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketStorageMapper mapper = sqlSession.getMapper(MarketStorageMapper.class);
        mapper.deleteByPrimaryKey(marketStorage.getId());

        sqlSession.commit();
        sqlSession.close();
        return BaseRespVo.ok();
    }
}


